#！/bin/bash
#################################################################
# @Description: 用例名称：内存管理-内存容量，测试目的：测试操作系统支持最大内存不小于4TB
# @Date : 2023-11-22
# @Last Modified by :
# @Last Modified time:
#################################################################
res=0
capacity=0
bc_installed=1

#0.安装bc软件包
echo "---该用例测试操作系统支持最大内存不小于4TB,请确保机器上空闲磁盘总容量之和不小于4TB---"
echo "0.安装bc软件包"
yum install bc -y | grep "Installed:" > /dev/null 2>&1 && bc_installed=0
[ "$bc_installed" = "0" ] && echo "bc软件包安装成功" || echo "bc软件包已安装"

#1.输入空闲磁盘名称
#1.1查看是否输入空闲磁盘名称
echo ""
echo "1.请输入1个或多个空闲磁盘名称并用空格隔开：(格式:/dev/sdb /dev/sdc)"
read -p "> " free_disks
IFS=" " read -ra free_disk <<< "$free_disks"
if [ -z "$free_disks" ]; then
    echo "没有输入空闲磁盘名称,退出脚本"
    exit 1
fi
#1.2判断空闲磁盘容量之和是否不小于4TB
for disk in "${free_disk[@]}"; do
	disk=$(echo $disk | awk -F '/' '{print $3}')
    num=$(lsblk | grep $disk | wc -l)
    if [[ $num > 1 ]]; then
		echo "---------lsblk | grep $disk---------"
		lsblk | grep $disk
		echo "---------lsblk | grep $disk---------"
        echo "${disk}磁盘有分区，请格式化该分区后再次运行脚本"
        exit 1
    elif [[ $num < 1 ]]; then
        echo "没有找到${disk}磁盘,重新运行脚本"
        exit 1
    fi
    disk_size=$(lsblk --bytes | grep $disk | awk '{sum = $4/1024/1024/1024/1024} END {printf "%.1f", sum}')
    capacity=$(echo "$capacity + $disk_size" | bc)
done
if (( $(echo "$capacity <= 4" | bc -l) )); then
    echo "空闲磁盘总容量之和为${capacity}TB,不符合测试要求,请添加磁盘后再次运行脚本"
    exit 1
fi
old_mem=$(free -k | grep "Swap:" | awk -F '[[:space:]]+' '{sum = $2/1024/1024} END {printf "%.1f", sum}')
if (( $(echo "$old_mem > 4096" | bc -l) )); then
    echo "当前swap分区容量大于4TB,已满足测试要求,退出脚本"
	echo -e "\033[32m-----------------test PASSED----------------\033[0m"
    exit 0
else
	echo "当前机器内存为：${old_mem}G"
	echo "--------------------free -h--------------------"
	free -h
	echo "--------------------free -h--------------------"
fi

#2.格式化
echo ""
echo "2.格式化空闲磁盘成swap分区......"
for disk in "${free_disk[@]}"; do
    mkswap -f $disk > /dev/null 2>&1 || res=2
    [ "$res" = "0" ] && echo "格式化磁盘${disk}成swap分区成功!"
done
if [ "$res" = "0" ]; then
	echo "已格式化所有空闲磁盘!"
	echo "--------------------blkid查看swap信息--------------------"
	blkid -t TYPE=swap
	echo "--------------------blkid查看swap信息--------------------"
else
	echo "格式化磁盘失败,退出脚本"
	exit 1
fi

#3.把新格式化的swap分区打开
echo ""
echo "3.打开新格式化的swap分区......"
for disk in "${free_disk[@]}"; do
    swapon $disk > /dev/null 2>&1 || res=3
    [ "$res" = "0" ] && echo "打开${disk}格式化的swap分区成功!"
done
if [ "$res" = "0" ]; then
	echo "已打开所有swap分区!"
	echo "--------------------lsblk查看磁盘--------------------"
	lsblk
	echo "--------------------lsblk查看磁盘--------------------"
	echo "--------------------free -h查看swap容量--------------------"
	free -h
	echo "--------------------free -h查看swap容量--------------------"
else
	echo "swap分区打开失败,退出脚本"
	for disk in "${free_disk[@]}"; do
		swapoff $disk > /dev/null 2>&1
		wipefs -a $disk > /dev/null 2>&1
	done
	exit 1
fi

#查看swap分区容量
cur_mem=$(free -k | grep "Swap:" | awk -F '[[:space:]]+' '{sum = $2/1024/1024/1024} END {printf "%.1f", sum}')
echo "当前机器内存为：${cur_mem}T"
if (( $(echo "$cur_mem >= 4" | bc -l) )); then
    echo "当前机器内存为：${cur_mem}T,内存不小于4TB,测试成功!"
else
    echo "当前机器内存为：${cur_mem}T,测试失败!"
	res=3
fi

#4.清理环境
echo ""
echo "4.清理环境"
for disk in "${free_disk[@]}"; do
    swapoff $disk > /dev/null 2>&1 || res=4
	wipefs -a $disk > /dev/null 2>&1 || res=4
    [ "$res" = "0" ] && echo "关闭${disk} swap分区成功!"
done
[ "$res" = "0" ] && echo "清理环境成功!"
[ "$res" = "0" ] && echo -e "\033[32m-----------------test PASSED----------------\033[0m" || echo -e "\033[31m-----------------test FAILD----------------\033[0m"

exit $res